package com.tfjybj.itoo.teach.provider.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.AcademyEntity;
import com.tfjybj.itoo.teach.entity.CourseScheduleEntity;
import com.tfjybj.itoo.teach.entity.StudentEntity;
import com.tfjybj.itoo.teach.model.ArrangeCourseModel;
import com.tfjybj.itoo.teach.model.ClassModel;
import com.tfjybj.itoo.teach.model.CourseIsArrangedModel;
import com.tfjybj.itoo.teach.model.CourseScheduleModel;
import com.tfjybj.itoo.teach.provider.service.AcademyService;
import com.tfjybj.itoo.teach.provider.service.CourseScheduleService;
import com.tfjybj.itoo.teach.provider.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * CourseScheduleController
 * courseSchedule表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"课程表接口"})
@RequestMapping(value = "/courseSchedule")
@RestController
public class CourseScheduleController {

    @Resource
    private CourseScheduleService courseScheduleService;

    //region 模板生成：基本增删改
    @Resource
    private StudentService studentService;

    @Resource
    private AcademyService academyService;


    /**
     * 添加
     *
     * @param model CourseScheduleModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody CourseScheduleModel model) {
        if (StringUtils.isEmpty(model.getClassId())) {
            return ItooResult.build(ItooResult.FAIL, "classId为空");
        }
        if (StringUtils.isEmpty(model.getTeachClassId())) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId为空");
        }
        if (StringUtils.isEmpty(model.getCellTimeId())) {
            return ItooResult.build(ItooResult.FAIL, "cellTimeId为空");
        }
        if (StringUtils.isEmpty(model.getWeekId())) {
            return ItooResult.build(ItooResult.FAIL, "weekId为空");
        }
        if (StringUtils.isEmpty(model.getRoomId())) {
            return ItooResult.build(ItooResult.FAIL, "roomId为空");
        }
        if (StringUtils.isEmpty(model.getTeacherTeachId())) {
            return ItooResult.build(ItooResult.FAIL, "teacherTeachId为空");
        }
        if (StringUtils.isEmpty(model.getSemesterId())) {
            return ItooResult.build(ItooResult.FAIL, "semesterId为空");
        }
        CourseScheduleEntity courseScheduleEntity = new CourseScheduleEntity();
        BeanUtils.copyProperties(model, courseScheduleEntity);
        courseScheduleService.save(courseScheduleEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return itooResult 是否删除成功
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        courseScheduleService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return itooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        courseScheduleService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model CourseScheduleModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改courseSchedule")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody CourseScheduleModel model) {
        if (StringUtils.isEmpty(model.getClassId())) {
            return ItooResult.build(ItooResult.FAIL, "classId为空");
        }
        if (StringUtils.isEmpty(model.getTeachClassId())) {
            return ItooResult.build(ItooResult.FAIL, "teachClassId为空");
        }
        if (StringUtils.isEmpty(model.getCellTimeId())) {
            return ItooResult.build(ItooResult.FAIL, "cellTimeId为空");
        }
        if (StringUtils.isEmpty(model.getWeekId())) {
            return ItooResult.build(ItooResult.FAIL, "weekId为空");
        }
        if (StringUtils.isEmpty(model.getRoomId())) {
            return ItooResult.build(ItooResult.FAIL, "roomId为空");
        }
        if (StringUtils.isEmpty(model.getTeacherTeachId())) {
            return ItooResult.build(ItooResult.FAIL, "teacherTeachId为空");
        }
        if (StringUtils.isEmpty(model.getSemesterId())) {
            return ItooResult.build(ItooResult.FAIL, "semesterId为空");
        }
        CourseScheduleEntity courseScheduleEntity = new CourseScheduleEntity();
        BeanUtils.copyProperties(model, courseScheduleEntity);
        courseScheduleService.updateById(courseScheduleEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }


    /**
     * 根据机构/学院id获取机构或学院信息
     * @param id 主键id
     * @author 杨晓慧
     * @since 2.0.0 2018年12月19日11:36:54
     */
    @ApiOperation(value = "根据机构/学院id获取机构或学院信息")
    @GetMapping(value = {"/findInstitutionById/{id}"})
    public ItooResult findInstitutionById(@ApiParam(value = "二级学院或机构主键id", required = true) @PathVariable String id) {
        AcademyEntity byId = academyService.getById(id);
        if (!StringUtils.isEmpty(byId)) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", byId);
        }else{
            return ItooResult.build(ItooResult.FAIL,"学院或机构不存在");
        }
    }


    /**
     * 根据id查找CourseSchedule
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        CourseScheduleEntity courseScheduleEntity = courseScheduleService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseScheduleEntity);
    }

    /**
     * 分页查询所有CourseSchedule
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨晓慧
     * @since 2.0.0 2018年11月20日15:17:52
     */
    @ApiOperation(value = "分页查询所有CourseSchedule")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<CourseScheduleModel> courseSchedules = courseScheduleService.queryPageCourseAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseSchedules);
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */


    /**
     * 高级搜索条件查询课表信息
     * @param strLikeMap 教师，班级，课程信息Map
     * @return 课程表信息列表
     * @author 杨晓慧
     * @since 2.0.0 2018年11月27日11:00:25
     */
    @ApiOperation(value = "高级搜索条件查询课表信息（教师，班级，课程）", notes = "根据教师，班级，课程其中任一或多个条件查询 ")
    @PostMapping(value = {"/queryScheduleByStrLike/{pageNo}/{pageSize}"})

    public ItooResult queryScheduleByStrLike(@RequestBody Map strLikeMap, @PathVariable Integer pageNo, @PathVariable Integer pageSize){
        if (!MapUtils.isEmpty(strLikeMap)) {
            PageInfo<CourseScheduleModel> scheduleModelList = courseScheduleService.queryScheduleByStrLike(strLikeMap,pageNo, pageSize);
            return ItooResult.build(ItooResult.SUCCESS, "查询课表信息成功！", scheduleModelList);
        }else{
            return ItooResult.build(ItooResult.FAIL, "查询条件参数为空！");
        }
    }

    /**
     * 查询当前排课状态信息
     *
     * @param condition 当前排课状态信息集和（起始周次，学年学期，行政班集和，教室id,教师id）
     * @return 返回已经被占用的星期和节次（包括教室，教师，行政班）
     */
    @ApiOperation(value = "查询当前排课状态信息", notes = "检测教室，教室，行政班上课已排课情況")
    @PostMapping(value = {"/queryArrangeCourseInfo"})
        public ItooResult queryArrangeCourseInfo(@RequestBody Map condition) {
        if (!MapUtils.isEmpty(condition)) {
            if (condition.containsKey("startWeek") && condition.containsKey("endWeek") && condition.containsKey("semesterId")) {
                List<CourseScheduleModel> courseScheduleModelList = courseScheduleService.queryArrangeCourseInfo(condition);
                if (!CollectionUtils.isEmpty(courseScheduleModelList)) {
                    return ItooResult.build(ItooResult.SUCCESS, "查询排课状态信息成功！", courseScheduleModelList);
                } else {
                    if (CollectionUtils.isEmpty(courseScheduleModelList)){
                        return ItooResult.build(ItooResult.SUCCESS, "查询排课状态信息为空",courseScheduleModelList);
                    }else{
                        return ItooResult.build(ItooResult.FAIL, "查询排课状态信息失败！");
                    }
                }
            } else {
                return ItooResult.build(ItooResult.FAIL, "学年学期和起始周次信息不能为空！");
            }
        } else {
            return ItooResult.build(ItooResult.FAIL, "查询参数信息有误！");
        }
    }

    /**
     * 添加排课信息--杨晓慧--2018年11月17日
     *
     * @param arrangeCourseModel 新增的排课实体
     * @return 返回添加成功的排课记录条数
     */
    @ApiOperation(value = "添加排课信息", notes = "以每节课为一个单位，插入课程，教室，教师，行政班集和，星期节次信息，插入总数为行政班集和数量*课时数量（多条星期节次）")
    @PostMapping(value = {"/insertArrangeCourseInfo"})
    public ItooResult insertArrangeCourseInfo(@RequestBody ArrangeCourseModel arrangeCourseModel) {

        if (CollectionUtils.isEmpty(arrangeCourseModel.getCourseScheduleModelList())) {
            return ItooResult.build(ItooResult.FAIL, "要安排的星期周次信息不能为空！");
        }
        if (StringUtils.isEmpty(arrangeCourseModel.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "课程不能为空！");
        }
        if (StringUtils.isEmpty(arrangeCourseModel.getTeachClassId())) {
            return ItooResult.build(ItooResult.FAIL, "上课班不能为空！");
        }
        if (StringUtils.isEmpty(arrangeCourseModel.getTeacherId())) {
            return ItooResult.build(ItooResult.FAIL, "教师不能为空！");
        }
        if (StringUtils.isEmpty(arrangeCourseModel.getSemesterId())) {
            return ItooResult.build(ItooResult.FAIL, "学年学期信息不能为空！");
        }
        boolean insertNum = courseScheduleService.insertArrangeCourseInfo(arrangeCourseModel);
        return ItooResult.build(ItooResult.SUCCESS, "插入课表数据成功！", insertNum);
    }

    /**
     * 根据班级_id查询的所有待排课程
     *
     * @param classId 班级_id
     * @return courseAllList
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据《行政班_id》查询的所有《未排课程》", notes = "根据班级_id查询的courseList")
    @GetMapping(value = "/queryCourseAllByClassId")
    public ItooResult queryCourseAllByClassId(@RequestParam(required = false, defaultValue = "") String classId) {
        List<CourseIsArrangedModel> courseAllList = courseScheduleService.queryCourseAllByClassId(classId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseAllList);
    }

    /**
     * 根据StudentCode查询的课程表
     * @param studentCode 学生学号
     * @return courseScheduleList
     * @author 杨晓慧
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据《StudentCode》查询《已排课程表》", notes = "根据StudentCode查询的课程表")
    @GetMapping(value = "/queryCourseScheduleByStudentCode")
    public ItooResult queryCourseScheduleByStudentCode(@RequestParam(required = false, defaultValue = "") String studentCode) {

        QueryWrapper<StudentEntity> studentEntityQueryWrapper=new QueryWrapper<>();
        List<CourseScheduleModel> courseScheduleListAll=new ArrayList<>();

        //1.根据学生学号唯一确定该学生的信息
        studentEntityQueryWrapper.eq("code",studentCode);
        List<StudentEntity> studentEntityList= studentService.list(studentEntityQueryWrapper);
        //2.获取该学生所在的行政班id和该学生的id
        if (!CollectionUtils.isEmpty(studentEntityList)) {
            String classId= studentEntityList.get(0).getClassesId();
            String studentId= studentEntityList.get(0).getId();

            //3.根据学生id获取所在的所有上课班id集和（选修课上课班id集和）
            List<String> teachClassIds= courseScheduleService.queryTechClassIdByStudentId(studentId);

            //4. 根据行政班id查出该学生所在行政班的课表信息集和（必修课课表）
            List<CourseScheduleModel> courseScheduleList1= courseScheduleService.queryCourseScheduleByClassId(classId);

            //5.根据上课班id集和查询课程表信息集和（选修课课表）
            if (!CollectionUtils.isEmpty(teachClassIds)){
                //5.1 当上课班集和不为空时，查询该学生的选修课排课集和
                List<CourseScheduleModel> courseScheduleList2 =courseScheduleService.queryCourseScheduleByTeachClassIds(teachClassIds);
                if (!CollectionUtils.isEmpty(courseScheduleList2)) {
                    courseScheduleListAll.addAll(courseScheduleList2);
                }
            }
            //6. 将4和5两个集和合并组成该学生最终的课表信息
            if (!CollectionUtils.isEmpty(courseScheduleList1)) {
                courseScheduleListAll.addAll(courseScheduleList1);
            }

            return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseScheduleListAll);
        }else {
            return ItooResult.build(ItooResult.FAIL, "查询失败,学号信息有误");
        }
    }


    /**
     * 根据行政班id查询的课程表     *
     * @param classId 行政班id
     * @return courseScheduleList
     * @author 杨晓慧
     * @since 2.0.0 2018-11-18 15:54:10
     */
    @ApiOperation(value = "根据《行政班id》查询的《已排课程表》", notes = "根据行政班id查询的课程表")
    @GetMapping(value = "/queryCourseScheduleByClassId")
    public ItooResult queryCourseScheduleByClassId(@RequestParam(required = true, defaultValue = "") String classId) {
        if (!StringUtils.isEmpty(classId)) {
            List<CourseScheduleModel> courseScheduleList = courseScheduleService.queryCourseScheduleByClassId(classId);
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseScheduleList);
        }else{
            return ItooResult.build(ItooResult.FAIL, "查询失败，参数信息错误");
        }
    }



    /**
     * 根据teacherId查询的课程表
     *
     * @param teacherId 教师ID
     * @return courseScheduleList
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据《教师_Id》查询《已排课程表》", notes = "根据teacherId查询的课程表")
    @GetMapping(value = "/queryCourseScheduleByTeacherId")
    public ItooResult queryCourseScheduleByTeacherId(@RequestParam(required = false, defaultValue = "") String teacherId) {
        List<CourseScheduleModel> courseScheduleList = courseScheduleService.queryCourseScheduleByTeacherId(teacherId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", courseScheduleList);
    }


    /**
     * 根据上课班id集合来查询课程id集和
     * @param teachClassList 上课班id集和
     * @return 课程id集和
     * @author 杨晓慧
     * @since 2.0.0 2018-11-08 20:35:10
     */
    @ApiOperation(value = "根据《上课班id集合》来查询课程id集和")
    @PostMapping(value = {"/queryCourseIdsByTeachClassIds"})
    @ApiImplicitParam(name = "teachClassList", value = "teachClassList", dataType = "List<String>", required = true)

    public ItooResult queryCourseIdsByTeachClassIds(@RequestBody List<String> teachClassList) {
        if (CollectionUtils.isEmpty(teachClassList)) {
            return ItooResult.build(ItooResult.FAIL,"上课班集和不能为空");
        } else {
            List<String> listCourseId= courseScheduleService.queryCourseIdsByTeachClassIds(teachClassList);
            if (CollectionUtils.isEmpty(listCourseId)) {
                return ItooResult.build(ItooResult.SUCCESS,"查询课程id集和为空");
            }else{
                return ItooResult.build(ItooResult.SUCCESS,"查询课程id集和",listCourseId);
            }
        }
    }



    /**
     * 根据id集合来查询课程表集和
     * @param ids 课程id集和
     * @return 课程表集和_listCourseScheduleModel
     * @author 田成荣
     * @since 2.0.0 2018-11-08 20:35:10
     */
    @ApiOperation(value = "根据《课程id集和》来查询《已排课程表集和》")
    @PostMapping(value = {"/queryCourseScheduleByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<CourseScheduleModel>", required = true)

    public ItooResult queryCourseScheduleByIds(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ItooResult.build(ItooResult.FAIL,"课程id集和不能为空");
        } else {
            List<CourseScheduleModel> listCourseScheduleModel= courseScheduleService.queryCourseScheduleByIds(ids);
            if (CollectionUtils.isEmpty(listCourseScheduleModel)) {
                return ItooResult.build(ItooResult.SUCCESS,"查询课程id集和为空");
            }else{
                return ItooResult.build(ItooResult.SUCCESS,"查询课程id集和",listCourseScheduleModel);
            }
        }
    }


    /**
     * 根据课程id来查询是否排过课
     * @param courseId 课程id
     * @return 课程表信息
     * @author 杨晓慧
     * @since 2.0.0 2018年11月20日15:43:24
     */
    @ApiOperation(value = "根据《课程id》来查询《已排课程表信息》")
    @GetMapping(value = {"/queryScheduleByCourseId/{courseId}"})
    @ApiImplicitParam(name = "courseId", value = "courseId", dataType = "String", required = true)

    public ItooResult queryScheduleByCourseId(@PathVariable String courseId) {
        if (StringUtils.isEmpty(courseId)) {
            return ItooResult.build(ItooResult.FAIL,"课程id不能为空");
        } else {
            List<CourseScheduleModel> listSchedule= courseScheduleService.queryScheduleByCourseId(courseId);
            if (CollectionUtils.isEmpty(listSchedule)) {
                return ItooResult.build(ItooResult.SUCCESS,"查询课程表信息为空!",listSchedule);
            }else{
                return ItooResult.build(ItooResult.SUCCESS,"查询课程表信息成功!",listSchedule);
            }
        }
    }

    /**
     * 根据学院名称和专业名称查询行政班信息
     * @param strLike 模糊查询字段
     * @return 行政班列表
     * @author 杨晓慧
     * @since 2.0.0 2018年11月22日14:43:20
     */
    @ApiOperation(value = "根据《学院名称和专业名称》查询《行政班》信息")
    @GetMapping(value = {"/queryClassByAcmOrPfs"})
    public ItooResult queryClassByAcmOrPfs(@ApiParam(value="学院名称或专业名称") @RequestParam(required=false,name="strLike") String strLike,
                                           @ApiParam(value="学院id") @RequestParam(required=false,name="academyId") String academyId){
        List<ClassModel> classModelList= courseScheduleService.queryClassByAcmOrPfs(strLike,academyId);
        if (CollectionUtils.isEmpty(classModelList)) {
            return ItooResult.build(ItooResult.FAIL,"查询行政班信息为空！");
        }else{
            return ItooResult.build(ItooResult.SUCCESS, "查询成功!", classModelList);
        }
   }


    /**
     * 根据教室id查询该教室的课表情况
     * @param roomId 教室id
     * @return 返回该教室课表情况
     * @author 杨晓慧
     * @since 2.0.0 2018年12月3日15:40:30
     */
    @ApiOperation(value = "根据《教室id》查询课表信息")
    @GetMapping(value = {"/queryCourseScheduleByRoomId"})
    public ItooResult queryCourseScheduleByRoomId(@ApiParam(value="教室id") @RequestParam(name="roomId") String roomId){
        if (!StringUtils.isEmpty(roomId)) {
            List<CourseScheduleModel> list =courseScheduleService.queryCourseScheduleByRoomId(roomId);
            if (!CollectionUtils.isEmpty(list)) {
                return ItooResult.build(ItooResult.SUCCESS, "查询该教室课表信息成功！", list);
            }else{
                return ItooResult.build(ItooResult.SUCCESS, "该教室暂未安排上课！", list);
            }
        }else {
            return ItooResult.build(ItooResult.FAIL, "教室id为空！");
        }
    }







}
